/**
 * jims
 */
package com.jims.master.modules.operation.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.modules.oper.dao.ClinicItemDictDao;
import com.jims.master.modules.oper.dao.ClinicVsChargeDao;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.entity.ClinicVsCharge;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.oper.entity.PriceListLog;
import com.jims.master.modules.oper.service.PriceListService;
import com.jims.master.modules.operation.dao.OperationDictDao;
import com.jims.master.modules.operation.entity.OperationDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 手术项目Service
 *
 * @author ZhuQ
 * @version 2017-04-06
 */
@Service
@Transactional(readOnly = true)
public class OperationDictService extends CrudService<OperationDictDao, OperationDict> {
    @Autowired
    private ClinicVsChargeDao clinicVsChargeDao;
    @Autowired
    private PriceListService priceListService;
    @Autowired
    private ClinicItemDictDao clinicItemDictDao;
    @Autowired
    private PriceListDao priceListDao;


    public OperationDict get(String id) {
        return super.get(id);
    }



    public List<OperationDict> findList(OperationDict operationDict) {
        return super.findList(operationDict);
    }

    public Page<OperationDict> findPage(Page<OperationDict> page, OperationDict operationDict) {
        return super.findPage(page, operationDict);
    }

    @Transactional(readOnly = false)
    public int save(OperationDict operationDict) {

        int n=1;

        operationDict.setUnit("0");
        //组装priceList数据与log数据
        PriceList priceList = new PriceList();
        PriceListLog priceListLog = new PriceListLog();
        priceList.setItemClass("F");
        priceList.setItemName(operationDict.getOperationName());
        priceList.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(operationDict.getOperationName()));
        priceList.setItemSpec("/");        // 项目规格
        priceList.setUnits(operationDict.getUnit());        // 单位
        priceList.setPrice(operationDict.getPrice());        // 价格
        priceList.setPreferPrice(operationDict.getPreferPrice());        // 优惠价格
        priceList.setClassOnRcpt("G01");        // 费用分类
        priceList.setClassOnReckoning("G01");        // 核算项目分类
        priceList.setSubjCode("G01");        // 会计科目
        priceList.setClassOnMr("10");        // 病案首页分类
        priceList.setStartDate(new Date());        // 开始时间
        priceList.updateOrgId();
        priceList.preInsert();
        int i = priceListDao.getMaxItemCode(priceList.getOrgId(), priceList.getItemClass());
        String num = String.valueOf(i);
        String str = "00000";
        str = "F" + str.substring(0, 5 - num.length()) + num;
        priceList.setItemCode(str);        // 项目代码
        n=n*priceListDao.insert(priceList);
        n=n*priceListService.pubMethod(priceList);


        //组装clinicItemDict数据
        ClinicItemDict clinicItemDict = new ClinicItemDict();
        clinicItemDict.setItemClass("F");
        clinicItemDict.setItemName(operationDict.getOperationName());
        clinicItemDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(clinicItemDict.getItemName()));
        clinicItemDict.setItemCode(priceList.getItemCode());
        clinicItemDict.setPrice(operationDict.getPrice());
        clinicItemDict.setPreferPrice(operationDict.getPreferPrice());
        clinicItemDict.updateOrgId();
        clinicItemDict.preInsert();
        n=n*clinicItemDictDao.insert(clinicItemDict);

        //组装clinicVsCharge数据
        ClinicVsCharge clinicVsCharge = new ClinicVsCharge();
        clinicVsCharge.setClinicId(clinicItemDict.getId());
        clinicVsCharge.setChargeId(priceList.getId());
        clinicVsCharge.setAmount("1");
        clinicVsCharge.preInsert();
        n=n*clinicVsChargeDao.insert(clinicVsCharge);


        //组装operationDict数据
        operationDict.updateOrgId();
        operationDict.preInsert();
        operationDict.setId(priceList.getId());
        operationDict.setClinicId(clinicItemDict.getId());
        n=n*dao.insert(operationDict);

        return n;
    }

    @Transactional(readOnly = false)
    public int delete(OperationDict operationDict) {
        return super.delete(operationDict);
    }

    @Transactional(readOnly = false)
    public OperationDict findPriceByDict(String id){
    return dao.findPriceByDict(id);
    }
}